<template>
  <div class="page-bg">
    <front-header />
    <div class="main-content">
      <el-card class="appointment-card">
        <div class="title">预约专家</div>
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
          <el-form-item label="预约时间" prop="appointmentTime">
            <el-date-picker v-model="form.appointmentTime" type="datetime" style="width:100%" />
          </el-form-item>
          <el-form-item label="说明" prop="description">
            <el-input v-model="form.description" type="textarea" />
          </el-form-item>
          <el-form-item>
            <div class="btn-row">
              <el-button type="success" :loading="loading" @click="submit">提交预约</el-button>
              <el-button @click="goBack">返回</el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <front-footer />
  </div>
</template>
<script>
import { createAppointment } from '../../api/ExpertAppointmentController';
import FrontHeader from "@/components/front/FrontHeader.vue";
import FrontFooter from "@/components/front/FrontFooter.vue";
export default {
  components: {FrontFooter, FrontHeader},
  data() {
    const userInfo = JSON.parse(localStorage.getItem('frontUser') || '{}');
    return {
      userInfo,
      form: {
        expertId: Number(this.$route.params.expertId),
        userId: userInfo.id,
        status: 'pending',
        appointmentTime: '',
        description: ''
      },
      loading: false,
      rules: {
        appointmentTime: [ { required: true, message: '请选择预约时间', trigger: 'blur' } ],
        description: [ { required: true, message: '请填写说明', trigger: 'blur' } ]
      }
    };
  },
  mounted() {
    if (!this.userInfo || !this.userInfo.id) {
      this.$message.warning('请先登录');
      this.$router.push('/login');
    }
  },
  methods: {
    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return;
        this.loading = true;
        try {
          await createAppointment(this.form);
          this.$message.success('预约成功！');
          setTimeout(() => {
            this.$router.push({ name: 'ExpertList' });
          }, 800);
        } catch (e) {
          this.$message.error('预约失败，请稍后重试');
        } finally {
          this.loading = false;
        }
      });
    },
    goBack() {
      this.$router.back();
    }
  }
};
</script>
<style scoped>
.page-bg {
  min-height: 100vh;
  background: #f9f9f9;
  display: flex;
  max-height: 100%;
  flex-direction: column;
}
.main-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 32px 0 32px 0;
  max-height: 100%;
  overflow: auto;
}
.appointment-card {
  width: 420px;
  padding: 32px 32px 16px 32px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  background: #ffffff;
  border: 1px solid #eaeaea;
}
.title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 28px;
  text-align: center;
  color: #333333;
  position: relative;
  display: inline-block;
  left: 50%;
  transform: translateX(-50%);
}
.title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, #4caf50 0%, #81c784 100%);
  border-radius: 2px;
}
.btn-row {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 8px;
}
.el-form :deep(.el-form-item__label) {
  color: #333333;
}
.el-form :deep(.el-input__inner) {
  background: #ffffff;
  border: 1px solid #dcdfe6;
  color: #333333;
  border-radius: 8px;
}
.el-form :deep(.el-input__inner:focus) {
  border-color: #4caf50;
}
.el-form :deep(.el-textarea__inner) {
  background: #ffffff;
  border: 1px solid #dcdfe6;
  color: #333333;
  border-radius: 8px;
  min-height: 120px;
}
.el-form :deep(.el-textarea__inner:focus) {
  border-color: #4caf50;
}
.btn-row .el-button {
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s ease;
}
.btn-row .el-button--success {
  background: linear-gradient(90deg, #4caf50 0%, #81c784 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}
.btn-row .el-button--success:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(76, 175, 80, 0.4);
  background: linear-gradient(90deg, #3b9c3f 0%, #6ebb71 100%);
}
.btn-row .el-button--default {
  border-color: #4caf50;
  color: #4caf50;
  background: transparent;
}
.btn-row .el-button--default:hover {
  color: #ffffff;
  background: rgba(76, 175, 80, 0.1);
  border-color: #4caf50;
}
@media (max-width: 600px) {
  .appointment-card {
    width: 95vw;
    padding: 24px 16px 16px 16px;
  }
  .main-content {
    margin: 16px 0 16px 0;
  }
  .btn-row {
    flex-direction: column;
  }
  .btn-row .el-button {
    width: 100%;
  }
}
</style> 